package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.adapter.NumericWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.Calendar;

public class BottomTimeDialog extends BottomPopupView {

    private DialogClickListener mDialogClickListener;
    private TextView tvLabel;
    private TextView tvSelectedTime;
    private WheelView wv_first;
    private WheelView wv_second;
    private WheelView wv_third;
    private NumericWheelAdapter mFirstNumericWheelAdapter;
    private NumericWheelAdapter mSecondNumericWheelAdapter;
    private NumericWheelAdapter mThirdNumericWheelAdapter;
    private Calendar selected;

    public BottomTimeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_wheel_view;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        tvLabel = findViewById(R.id.tv_label);
        tvSelectedTime = findViewById(R.id.tv_selected_time);
        wv_first = findViewById(R.id.options1);
        wv_second = findViewById(R.id.options2);
        wv_third = findViewById(R.id.options3);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.onConfirmClick(tvSelectedTime.getText().toString());
                }
            }
        });
        tvLabel.setText("生日选择");
        setStyle();
        initWheelView();
    }

    private void setStyle() {
        wv_first.setGravity(Gravity.CENTER);
        wv_first.setCyclic(false);
        wv_first.setAlphaGradient(true);
//        wv_first.setLabelOffset(5);
        wv_first.setItemsVisibleCount(5);
        wv_first.setDividerColor(0x00000000);
        wv_first.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_first.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_second.setGravity(Gravity.CENTER);
        wv_second.setCyclic(false);
        wv_second.setAlphaGradient(true);
//        wv_second.setLabelOffset(5);
        wv_second.setItemsVisibleCount(5);
        wv_second.setDividerColor(0x00000000);
        wv_second.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_second.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_third.setGravity(Gravity.CENTER);
        wv_third.setCyclic(false);
        wv_third.setAlphaGradient(true);
//        wv_third.setLabelOffset(5);
        wv_third.setItemsVisibleCount(5);
        wv_third.setDividerColor(0x00000000);
        wv_third.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_third.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));
    }

    private void initWheelView(){
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        startDate.set(endDate.get(Calendar.YEAR) - 100, 0, 1);
        int startYear = startDate.get(Calendar.YEAR);
        if(selected == null){
            selected = Calendar.getInstance();
            selected.set(endDate.get(Calendar.YEAR) - 24, endDate.get(Calendar.MONTH), endDate.get(Calendar.DAY_OF_MONTH));
        }
        mFirstNumericWheelAdapter  = new NumericWheelAdapter(startDate.get(Calendar.YEAR), endDate.get(Calendar.YEAR));
        wv_first.setAdapter(mFirstNumericWheelAdapter);
        int selectedYear = selected.get(Calendar.YEAR);
        wv_first.setCurrentItem(selectedYear - startYear);

        int month = selected.get(Calendar.MONTH);
        int maxMonth = 0;
        if(selectedYear == endDate.get(Calendar.YEAR)){
            maxMonth = endDate.get(Calendar.MONTH) + 1;
        }else{
            maxMonth = 12;
        }
        mSecondNumericWheelAdapter  = new NumericWheelAdapter(1, maxMonth);
        wv_second.setAdapter(mSecondNumericWheelAdapter);
        wv_second.setCurrentItem(month);

        mThirdNumericWheelAdapter  = new NumericWheelAdapter(1, selected.getActualMaximum(Calendar.DAY_OF_MONTH));
        wv_third.setAdapter(mThirdNumericWheelAdapter);
        wv_third.setCurrentItem(selected.get(Calendar.DAY_OF_MONTH) - 1);
        wv_first.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                tvSelectedTime.setText(formatText());
                int selectedMonth = wv_second.getCurrentItem();
                int selectedDay = wv_third.getCurrentItem();
                int selectedYear = Integer.valueOf((String) mFirstNumericWheelAdapter.getItem(wv_first.getCurrentItem()));
                selected.set(selectedYear, selectedMonth, 1);
                int maxMonth = 0;
                if(selectedYear == endDate.get(Calendar.YEAR)){
                    maxMonth = endDate.get(Calendar.MONTH) + 1;
                }else{
                    maxMonth = 12;
                }
                mSecondNumericWheelAdapter = new NumericWheelAdapter(1, maxMonth);
                wv_second.setAdapter(mSecondNumericWheelAdapter);
                wv_second.setCurrentItem(selectedMonth > maxMonth ? maxMonth - 1 : selectedMonth);
                int maxDay = 0;
                if(selectedYear == endDate.get(Calendar.YEAR)
                        && (selected.get(Calendar.MONTH) >= endDate.get(Calendar.MONTH))){
                    maxDay = endDate.get(Calendar.DAY_OF_MONTH);
                }else{
                    maxDay = selected.getActualMaximum(Calendar.DAY_OF_MONTH);
                }
                mThirdNumericWheelAdapter = new NumericWheelAdapter(1, maxDay);
                wv_third.setAdapter(mThirdNumericWheelAdapter);
                wv_third.setCurrentItem(selectedDay > maxDay ? maxDay - 1 : selectedDay);
            }
        });
        wv_second.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selectedYear = Integer.valueOf((String) mFirstNumericWheelAdapter.getItem(wv_first.getCurrentItem()));
                int selectedDay = wv_third.getCurrentItem();
                selected.set(selectedYear, index, 1);
                int max = 0;
                if((selectedYear == endDate.get(Calendar.YEAR))
                && (selected.get(Calendar.MONTH) >= endDate.get(Calendar.MONTH))){
                    max = endDate.get(Calendar.DAY_OF_MONTH) - 1;
                }else{
                    max = selected.getActualMaximum(Calendar.DAY_OF_MONTH);
                }
                mThirdNumericWheelAdapter = new NumericWheelAdapter(1, max);
                wv_third.setAdapter(mThirdNumericWheelAdapter);
                wv_third.setCurrentItem(selectedDay > max ? max : selectedDay);
                tvSelectedTime.setText(formatText());
            }
        });
        wv_third.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                tvSelectedTime.setText(formatText());
            }
        });
        tvSelectedTime.setText(formatText());
    }
    
    private String formatText(){
        return wv_first.getAdapter().getItem(wv_first.getCurrentItem()) + "/" + wv_second.getAdapter().getItem(wv_second.getCurrentItem()) + "/" + wv_third.getAdapter().getItem(wv_third.getCurrentItem());
    }

    public void setSelectDate(String date){
        if(TextUtils.isEmpty(date)){
            return;
        }
        String[] ymd = date.split("/");
        selected = Calendar.getInstance();
        selected.set(Integer.valueOf(ymd[0]), Integer.valueOf(ymd[1]) - 1, Integer.valueOf(ymd[2]));
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(String time);
    }
}
